<style>
  .layui-form-label.required:before {
    /* 这种写法也可以*/
    content: " *";
    color: red;
  }
  .open-form .layui-form-label {
    width: 110px;
  }
  .open-form .layui-input-block {
    margin-left: 140px;
  }
</style>
<script type="text/javascript" src="../../../admin/assets/js/pdfobject.min.js"></script>
<!-- 表单弹窗 -->
<form lay-filter="proTaskForm" class="layui-form proTaskForm open-form">
  <div class="open-model" id="proTaskFormDiv">
    <div class="open-auto">
      <fieldset class="layui-elem-field layui-field-title">
        <legend>客户信息产品信息</legend>
      </fieldset>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label required">来源：</label>
          <div class="layui-input-block">
            <input name="source" disabled type="text" class="layui-input" required autocomplete="off" value="客户订单" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">客户名称：</label>
          <div class="layui-input-block">
            <input type="text" id="customerName" disabled name="customerName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">客户产品编码：</label>
          <div class="layui-input-block">
            <input type="text" id="cusProductNo" disabled name="cusProductNo" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">产品图号：</label>
          <div class="layui-input-block">
            <div id="addHeaderPdf" class="companyFormPhotos"></div>
            <input name="attachmentPdf" id="attachmentPdf" type="hidden" class="layui-input" lay-verify="attachmentPdf" />
            <label id="fileName" hidden="hidden" style="margin-left: 34px"></label>
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label required">物资类别：</label>
          <div class="layui-input-block">
            <input type="text" id="productTypeName" disabled name="productTypeName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">订单编号：</label>
          <div class="layui-input-block">
            <input type="text" id="orderCode" disabled name="orderCode" lay-filter="orderCode" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">计划交货时间：</label>
          <div class="layui-input-block">
            <input type="text" id="planGivenTime" disabled name="planGivenTime" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">状态：</label>
          <div class="layui-input-block">
            <input type="text" id="statusName" disabled name="statusName" class="layui-input" lay-filter="statusName" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label required">创建人：</label>
          <div class="layui-input-block">
            <input type="text" id="creator" disabled name="creator" class="layui-input" lay-filter="creator" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">创建时间：</label>
          <div class="layui-input-block">
            <input type="text" id="createTime" disabled name="createTime" class="layui-input" lay-filter="createTime" />
          </div>
        </div>
      </div>
      <fieldset class="layui-elem-field layui-field-title">
        <legend>关联内部产品信息</legend>
      </fieldset>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label">产品类别：</label>
          <div class="layui-input-block">
            <input name="productTypeName" disabled type="text" class="layui-input" lay-verify="required" />
            <input id="productTypeId" name="productTypeId" type="hidden" value="' + d.proTypeId + '" lay-verify="required" />
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label">产品性质：</label>
          <div class="layui-input-block">
            <input name="productNatureName" disabled type="text" class="layui-input" lay-verify="required" />
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label">是否关联新料号：</label>
          <div class="layui-input-block">
            <input name="isUseOrAdd" disabled type="text" class="layui-input" lay-verify="required" />
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label">内部SKU号：</label>
          <div class="layui-input-block" id="SkuInfo">
            <input id="productDetailId" name="productDetailId" type="hidden" />
            <input id="inSkuCode" name="inSkuCode" disabled type="text" class="layui-input" lay-verify="required" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label">内部SKU名称：</label>
          <div class="layui-input-block">
            <input type="text" id="inSkuName" disabled name="inSkuName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">产品属性：</label>
          <div class="layui-input-block">
            <input name="assemblyNatureName" disabled type="text" class="layui-input" lay-verify="required" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">内部规格码：</label>
          <div class="layui-input-block">
            <input type="text" id="inSkuSpec" disabled name="inSkuSpec" class="layui-input" />
          </div>
        </div>
        <!--        <div class="layui-col-md3">-->
        <!--            <label class="layui-form-label">计量单位：</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="inSkuUnitName" disabled type="text" class="layui-input"-->
        <!--                       lay-verify="required"/>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="layui-col-md3">
          <label class="layui-form-label">基础单位：</label>
          <div class="layui-input-block">
            <input name="inUnitName" disabled type="text" class="layui-input" lay-verify="required" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label required">计量方式(主)：</label>
          <div class="layui-input-block">
            <input type="text" id="mainCalWayName" readonly="readonly" name="mainCalWayName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">包装单位(主)：</label>
          <div class="layui-input-block">
            <input type="text" id="mainUnitName" readonly="readonly" name="mainUnitName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label required">换算率(主)：</label>
          <div class="layui-input-block">
            <input type="text" id="mainUnitRate" readonly="readonly" name="mainUnitRate" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">计量方式(辅)：</label>
          <div class="layui-input-block">
            <input type="text" id="subCalWayName" readonly="readonly" name="subCalWayName" class="layui-input" />
          </div>
        </div>
        <!--        <div class="layui-col-md3">-->
        <!--            <label class="layui-form-label">基础单位数量：</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input type="text" disabled id="inUnitCount" name="inUnitCount" class="layui-input">-->
        <!--            </div>-->
        <!--        </div>-->
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label">计量单位(辅)：</label>
          <div class="layui-input-block">
            <input type="text" id="subUnitName" readonly="readonly" name="subUnitName" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">换算率(辅)：</label>
          <div class="layui-input-block">
            <input type="text" id="subUnitRate" readonly="readonly" name="subUnitRate" class="layui-input" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">成本核算类型：</label>
          <div class="layui-input-block">
            <select id="costCheckType" disabled name="costCheckType" lay-filter="costCheckType" class="layui-input borderNone"></select>
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">是否质检：</label>
          <div class="layui-input-block">
            <select id="checkStatus" disabled name="checkStatus" lay-filter="checkStatus" class="layui-input borderNone"></select>
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label">是否料箱管理：</label>
          <div class="layui-input-block">
            <select id="boxStatus" disabled name="boxStatus" lay-filter="boxStatus" class="layui-input borderNone"></select>
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">说明：</label>
          <div class="layui-input-block">
            <input type="text" id="inRemark" name="inRemark" class="layui-input" />
          </div>
        </div>
      </div>
      <fieldset class="layui-elem-field layui-field-title">
        <legend>其他信息</legend>
      </fieldset>
      <div class="layui-form-item layui-row">
        <div class="layui-tab" lay-filter="test-handle" lay-allowclose="true">
          <ul class="layui-tab-title">
            <li lay-id="YSBZ">产品验收标准</li>
            <li lay-id="BOMGUIHUA">MRP物料BOM规划</li>
            <li lay-id="GCGUIHUA">产品工程规划</li>
          </ul>
          <div class="layui-tab-content">
            <div class="layui-tab-item" id="YSBZBox">
              <div class="layui-form-item layui-row">
                <label class="layui-form-label">版本编号：</label>
                <input style="border: none" type="text" value="BCZ380C9.2.1.4.1.1.1-2" disabled id="infoVersion" name="infoVersion" class="layui-input" />
              </div>
              <div class="layui-form-item layui-row">
                <div class="layui-col-md5">
                  <div id="TuZhiPdf" style="height: 100%; margin-left: 10px"></div>
                </div>
                <div class="layui-col-md7">
                  <div class="layui-form-item layui-row">
                    <button type="button" id="addTestItem" class="layui-btn" lay-filter="addTestItem" lay-submit>添加检测项</button>
                    <table class="layui-table" id="testItemTable" lay-filter="testItemTable"></table>
                  </div>
                  <div class="layui-form-item text-right">
                    <button class="layui-btn" lay-filter="saveTestItem" type="button" id="saveTestItem">保存检测项</button>
                  </div>
                </div>
              </div>
            </div>
            <div class="layui-tab-item" id="bom_info">
              <div class="layui-form-item layui-row">
                <div class="layui-form-item layui-row">
                  <button type="button" id="addBomSKu" class="layui-btn" lay-filter="addBomSKu" lay-submit>添加子件物料</button>
                  <table class="layui-table" id="bomTreeTableInfo" lay-filter="bomTreeTableInfo"></table>
                </div>
              </div>
            </div>
            <div class="layui-tab-item" id="gy">
              <!-- <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                  <label class="layui-form-label">选择主材：</label>
                  <div class="layui-input-block">
                    <select id="mainSkuId" lay-search="" name="mainSkuId" lay-filter="mainSkuId" class="common-select-search"></select>
                  </div>
                </div>
              </div> -->
              <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                  <label class="layui-form-label">选择工艺路线：</label>
                  <div class="layui-input-block">
                    <select id="processLineTemplate" lay-search="" name="processLineTemplate" lay-filter="processLineTemplate" class="common-select-search"></select>
                  </div>
                </div>
              </div>
              <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                  <label class="layui-form-label">关联模具图纸：</label>
                  <div class="layui-input-block">
                    <select id="isRelevance" name="isRelevance" lay-filter="isRelevance" class="common-select-search">
                      <option value="0">不关联</option>
                      <option value="1">关联模具图纸</option>
                    </select>
                  </div>
                </div>
                <div class="layui-col-md9" id="moldUploadInfo">
                  <div class="layui-col-md4">
                    <label class="layui-form-label">模具图纸：</label>
                    <div class="layui-input-block">
                      <div id="fileUploadMoldBtn" class="layui-btn" style="float: left; margin-left: 3px">
                        <i class="layui-icon">&#xe681;</i>
                        上传文件
                      </div>
                      <br />
                      <div id="addMoldHeaderPdf" style="margin-top: -16px" class="companyFormPhotos"></div>
                      <input name="attachmentMoldPdf" id="attachmentMoldPdf" type="hidden" class="layui-input" />
                    </div>
                  </div>
                  <div class="layui-col-md7">
                    <label class="layui-form-label">模具图纸备注：</label>
                    <div class="layui-input-block">
                      <textarea id="moldRemark" name="moldRemark" placeholder="模具图纸备注" class="layui-textarea" autocomplete="off" />
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-form-item layui-row">
                <div class="layui-col-md4">
                  <button type="button" id="addProcessOne" class="layui-btn" lay-filter="addProcessOne" lay-submit>添加工序</button>
                  <table class="layui-table" id="productProjectTable" lay-filter="productProjectTable"></table>
                </div>
              </div>
              <!-- <div class="layui-form-item text-right">
                <button class="layui-btn" lay-filter="saveProductProject" type="button" id="saveProjectUp">保存产品规划</button>
              </div> -->
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button class="layui-btn layui-btn-normal" id="submitProofread" type="button">提交校对</button>
      <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
    </div>
  </div>
</form>

<script type="text/html" id="viewAttachmentHtml2">
  <form lay-filter="imgModifyForm" class="layui-form model-form" style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;">
    <div class="open-details-box" style="height: calc(100% - 75px);">
      <div class="layui-row table-bor " style="margin-top: 15px;">
        <div class="layui-form-item layui-row">
          <label class="layui-form-label required">产品图纸：</label>
          <div class="layui-input-block">
            <div id="example1" class="uploadImgStyle"></div>
            <div id="productPicBtn" class="layui-btn" style="float: left;">
              <i class="layui-icon">&#xe681;</i>
              上传新图纸
            </div>
            <input name="productPic" type="hidden" class="layui-input" lay-verify="" id="productPic" />
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md12">
            <label class="layui-form-label required">工艺说明：</label>
            <div class="layui-input-block">
              <textarea id="processDescription" name="processDescription" placeholder="工艺说明" class="layui-textarea" autocomplete="off" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
      <button class="layui-btn" lay-filter="imgModifyFormSubmit" lay-submit type="button">保存</button>
    </div>
  </form>
</script>

<script type="text/html" id="viewAttachmentHtml">
  <div id="example" style="height: 100%;"></div>
</script>

<!-- 工艺表格操作列 -->
<script type="text/html" id="productProcessTableBar">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="checkSku">选择成品</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delProcess">删除</a>
</script>

<!-- 质检表格操作列 -->
<script type="text/html" id="testItemTableBar">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delTestItem">删除</a>
</script>

<!-- bom表格操作列 -->
<script type="text/html" id="bomProductPlanningTableBar">
  <!-- {{# if(d.assemblyNature != '3'){ }} -->
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="addDownInfo">添加下级Bom</a>
  <!-- {{#}}} -->
  <!--    {{# if(d.assemblyNature == '3'){ }}-->
  <!--    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="blueprints">图纸管理</a>-->
  <!--    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="processRoute">工艺路线</a>-->
  <!--    {{#}}}-->
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="removeInfo">移除</a>
</script>
<script type="text/html" id="skuSelectDiv">
  <div style="padding: 10px;position: relative;">
    <table class="layui-table" id="skuSelectTable" lay-filter="skuSelectTable"></table>
    <button class="layui-btn" style="position: absolute;right: 50px;bottom: -50px;" id="skuSelectConfirm">确认选择</button>
  </div>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "tableTreeDj", "form", "table", "laydate", "admin", "laytpl", "upload", "config"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var config = layui.config;
    var upload = layui.upload;
    var table = layui.table;
    var admin = layui.admin;
    var form = layui.form;
    var laydate = layui.laydate;
    var tableTree = layui.tableTreeDj;
    var element = layui.element;
    let projectTableData = [];
    let testItemData = [];
    let moldName = "";
    let customerName = "";
    let fileName = "";
    let tzId;
    let tzVersion;
    let data = admin.getTempData("resProTaskData");
    let bom_tab = document.querySelector(".layui-tab-title li:nth-child(2)");
    let bom_content = document.querySelector(".layui-tab-content .layui-tab-item:nth-child(2)");
    let gx_tab = document.querySelector(".layui-tab-title li:nth-child(3)");
    let gx_content = document.querySelector(".layui-tab-content .layui-tab-item:nth-child(3)");
    const nowData = { ...parentData };
    if (nowData.eventName === "detail") {
      admin.setFormDisable("#proTaskFormDiv", true);
      $("#submitProofread").hide();
      $("#addBomSKu").hide();
    }
    if (nowData.type == "bom") {
      element.tabDelete("test-handle", "YSBZ");
      element.tabDelete("test-handle", "GCGUIHUA");
      element.tabChange("test-handle", "BOMGUIHUA");
      $("#submitProofread").hide();
    } else if (nowData.type == "process") {
      element.tabDelete("test-handle", "YSBZ");
      element.tabDelete("test-handle", "BOMGUIHUA");
      element.tabChange("test-handle", "GCGUIHUA");
    }
    /**
     * 初始化sku主材
     */
    // admin.initDataSelectAndMap("enterprise/product/admin/productDetailWMS/list", {categoryType: "production",limit:9999}, "mainSkuId",null , "SKU物料", "codeAndName", null, null);

    /**
     * 初始化任务详情
     */

    let reqUrl = "";
    if (nowData.type == "process" && data.taskId) {
      reqUrl = "enterprise/res/admin/echnologyParamRecord/detail/" + data.taskId + "/" + data.id;
    } else {
      reqUrl = "enterprise/res/admin/planning/proTask/detail/" + data.id;
    }
    admin.req(reqUrl, {}, function (res) {
      if (res.code == 200) {
        let dataValue = res.data === undefined ? null : res.data;
        if (dataValue.processProjectData.length > 0) {
          dataValue.processLineTemplate = dataValue.processProjectData[0].templateId;
        }

        form.val("proTaskForm", dataValue);
        admin.initDataSelect(
          "enterprise/product/admin/processLineTemplate/list?page=1&limit=999",
          {},
          "processLineTemplate",
          dataValue.processLineTemplate ? dataValue.processLineTemplate : null,
          "模板",
          "name",
          "id"
        );
        admin.getDictList("costCheckType", "product_detail_cost_check_type", res.data.costCheckType === null ? null : res.data.costCheckType, "成本核算");
        admin.getDictList("checkStatus", "sys_yes_no", res.data.checkStatus === null ? null : res.data.checkStatus, "是否质检");
        admin.getDictList("boxStatus", "sys_yes_no", res.data.boxStatus === null ? null : res.data.boxStatus, "是否料箱管理");

        projectTableData = res.data.processProjectData;
        testItemData = res.data.testItemData;
        if (res.data.drawInfo) {
          tzId = res.data.drawInfo.id;
          tzVersion = res.data.drawInfo.versionName;
        }
        let name = res.data.cusProductFigure;
        let imagUrl = "";
        //初始化文件
        $("#addHeaderPdf").html(
          '<div class="attachments">' +
            '<div><img name="attachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' +
            name +
            "</span></div></div>"
        );
        $("#attachmentPdf").val(res.data.attachmentPdfUrl);
        imagUrl = res.data.attachmentPdfUrl;
        $("#fileName").text(res.data.cusProductFigure);
        $("#TuZhiPdf").click(function () {
          let json = {};
          json.attachment = imagUrl;
          json.remark = res.data.drawInfo.remark;
          json.orderProNo = res.data.cusProductNo;
          json.drawId = res.data.drawInfo.id;
          showViewAttachmentModel2(json);
        });
        if (imagUrl && imagUrl.indexOf(".pdf") !== -1) {
          PDFObject.embed(imagUrl, "#TuZhiPdf");
        } else {
          let attachmentHtml = "";
          imagUrl.split(",").forEach((attachmentUrl, index) => {
            attachmentHtml += '<div><div><img style="max-width:620px" src="' + attachmentUrl + '"></div></div>';
          });
          $("#TuZhiPdf").html(attachmentHtml);
        }
        //模具的
        if (res.data.drawInfo) {
          let drawInfos = res.data.drawInfo;
          if (drawInfos.isRelevance === "1") {
            $("#moldUploadInfo").show();
            $("#addMoldHeaderPdf").html(
              '<div class="moldAttachments">' +
                '<div><img name="moldAttachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' +
                res.data.drawInfo.moldDrawInfo.name +
                "</span></div></div>"
            );
            $("#attachmentMoldPdf").val(drawInfos.moldDrawInfo.fileUrl);
            $("#moldRemark").val(drawInfos.moldDrawInfo.remark);
            moldName = drawInfos.moldDrawInfo.name;
            $(".moldAttachments").click(function () {
              let json = {};
              json.attachment = drawInfos.moldDrawInfo.fileUrl;
              showViewAttachmentModel(json);
            });
          } else {
            $("#moldUploadInfo").hide();
          }
          $("#isRelevance").val(drawInfos.isRelevance);
          /**
           * 初始化sku主材
           */
          admin.initDataSelectAndMap(
            "enterprise/product/admin/productDetailWMS/list",
            { categoryType: "production", limit: 9999 },
            "mainSkuId",
            drawInfos.mainSkuId == null ? null : drawInfos.mainSkuId,
            "SKU物料",
            "codeAndName",
            null,
            null
          );
        } else {
          $("#moldUploadInfo").hide();
        }

        //初始化工序productProjectTable
        table.render({
          elem: "#productProjectTable",
          data: projectTableData,
          id: "productProjectTable",
          limit: 9999,
          cols: [
            [
              { type: "numbers", title: "序号" },
              {
                field: "name",
                align: "center",
                title: "工序名称",
                width: "10%",
                templet: admin.setTableInput("name", nowData.eventName === "detail")
              },
              { field: "proCode", sort: true, title: "产品工序编码", templet: admin.setTableInput("proCode", nowData.eventName === "detail") },
              { field: "sorted", sort: true, title: "工序排序", hide: true },
              { field: "content", sort: true, title: "工序工艺要求", hide: true },
              { field: "fixture", sort: true, title: "夹具", hide: true },
              { field: "measuringTools", sort: true, title: "量具", hide: true },
              { field: "hoursQuota", sort: true, title: "工时定额(分钟)", hide: true },
              { field: "salaryQuota", sort: true, title: "工资定额(元)", hide: true },
              { field: "productDetailId", sort: true, title: "成品料号", hide: true },
              { align: "center", toolbar: "#productProcessTableBar", title: "操作", width: 160, hide: nowData.eventName === "detail" }
            ]
          ],
          done: function () {
            admin.tableInputChange("productProjectTable");
            projectTableData = table.cache["productProjectTable"];
          }
        });

        //如果为空查询关联的
        if (Array.isArray(testItemData) && testItemData.length === 0) {
          admin.req("enterprise/product/admin/productItem/listByType", { productType: res.data.productTypeId }, function (res) {
            if (res.code == 200) {
              testItemData = res.data;
            }
            //初始化质检标准表
            table.render({
              elem: "#testItemTable",
              data: testItemData,
              id: "testItemTable",
              limit: 9999,
              cols: [
                [
                  { type: "numbers", title: "序号" },
                  {
                    field: "name",
                    edit: "text",
                    align: "center",
                    title: "检查项"
                  },
                  { field: "standard", edit: "text", sort: true, title: "标准" },
                  { field: "utensil", edit: "text", sort: true, title: "器具" },
                  { field: "way", edit: "text", sort: true, title: "方法" },
                  { field: "importance", edit: "text", sort: true, title: "重要程度" },
                  { field: "detectionRate", edit: "text", sort: true, title: "检测率" },
                  { align: "center", toolbar: "#testItemTableBar", title: "操作", width: 100 }
                ]
              ]
            });
          });
        } else {
          //初始化质检标准表
          table.render({
            elem: "#testItemTable",
            data: testItemData,
            id: "testItemTable",
            limit: 9999,
            cols: [
              [
                { type: "numbers", title: "序号" },
                {
                  field: "name",
                  edit: "text",
                  align: "center",
                  title: "检查项"
                },
                { field: "standard", edit: "text", sort: true, title: "标准" },
                { field: "utensil", edit: "text", sort: true, title: "器具" },
                { field: "way", edit: "text", sort: true, title: "方法" },
                { field: "importance", edit: "text", sort: true, title: "重要程度" },
                { field: "detectionRate", edit: "text", sort: true, title: "检测率" },
                { align: "center", toolbar: "#testItemTableBar", title: "操作", width: 100 }
              ]
            ]
          });
        }

        //初始化 bom
        // 渲染表格
        var bomTree = tableTree.render(
          {
            id: "#bomTreeTableInfo",
            elem: "#bomTreeTableInfo",
            url: config.base + "enterprise/res/admin/planning/proBom/bomTree/" + data.productDetailId,
            page: false,
            cellMinWidth: 100,
            where: { limit: 9999 },
            cols: [
              [
                { type: "numbers", title: "序号", fixed: "left" },
                { field: "productName", sort: true, title: "BOM产品名称" },
                { field: "productCode", sort: true, title: "产品编号" },
                { field: "productSpec", sort: true, title: "规格码" },
                { field: "productTypeName", sort: true, title: "物资类别" },
                { field: "assemblyNatureName", sort: true, title: "产品性质" },
                { field: "inSkuUnitName", sort: true, title: "计量单位", width: 90 },
                { field: "inUnitCount", edit: nowData.eventName === "detail" ? false : "text", sort: true, title: "需求数", width: 80 },
                { field: "remark", edit: nowData.eventName === "detail" ? false : "text", sort: true, title: "描述" },
                { align: "center", toolbar: "#bomProductPlanningTableBar", hide: nowData.eventName === "detail", title: "操作", width: 240 }
              ]
            ],
            done: function (ddd) {
              console.log("bomTree", ddd);
            }
          },
          { keyPid: "parentId", title: "productName" }
        );

        form.render(); //菜单渲染 把内容加载进去
      } else {
        layer.msg(res.msg, { icon: 2 });
      }

      // 显示编辑弹窗
      function showSkuListInfo(productDetailId, skuCode, flag, bomId) {
        let title = "客户产品规划管理";
        let parentInfo = {};
        parentInfo.productDetailId = productDetailId;
        parentInfo.flag = flag;
        parentInfo.skuCode = skuCode;
        parentInfo.bomId = bomId;
        admin.putTempData("bomSkuData", parentInfo);
        admin.formOpen({
          type: 1,
          area: ["1480px", "90%"],
          style: "font-size: 20px;",
          title: title,
          path: "components/res/planning/skuDetailResearchForm.html",
          end: function () {
            bomTree.reload({}, "data");
          }
        });
      }

      // 工具条点击事件
      table.on("tool(bomTreeTableInfo)", function (obj) {
        if (obj.event === "addDownInfo") {
          //添加下级
          //addDownInfo
          showSkuListInfo(obj.data.id, obj.data.productCode, "2", obj.data.bomId);
        } else if (obj.event === "removeInfo") {
          //移除
          //removeInfo
          removeSkuInfo(obj.data.bomId);
        }
      });

      //监听表格的修改事件
      table.on("edit(bomTreeTableInfo)", function (obj) {
        // 假设我们要根据username字段的变化来更新email字段
        if (obj.field === "inUnitCount" || obj.field === "remark") {
          let params = {};
          params.id = obj.data.bomId;
          params.count = obj.data.inUnitCount;
          params.remark = obj.data.remark;
          admin.req(
            "enterprise/res/admin/planning/proBom/updateBom",
            params,
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "POST"
          );
        }
      });

      //addBomSKu 添加子件物料的点击事件
      $("#addBomSKu").click(function () {
        showSkuListInfo(data.productDetailId, data.skuCode, "1", "");
      });

      //移除下级物料
      function removeSkuInfo(bomId) {
        layer.confirm(
          "确认要移除该子件吗？",
          {
            offset: "65px",
            skin: "layui-layer-admin"
          },
          function (i) {
            layer.close(i);
            layer.load(2);
            //添加bom子件信息
            admin.req(
              "enterprise/res/admin/planning/proBom/deleteBom/" + bomId,
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "DELETE"
            );
          }
        );
      }
    });

    //下拉款的选择事件
    form.on("select(processLineTemplate)", function (data) {
      let id = data.value;
      table.reload("productProjectTable", {
        data: []
      });
      if (id) {
        //查询工艺模板关联的工序
        admin.req("enterprise/product/admin/processLineTemplate/listAndProcess?type=1&templateId=" + id + "&page=1&limit=999", {}, function (res) {
          if (res.code == 200) {
            let processes = res.data;
            projectTableData = processes.map((v) => {
              return {
                name: v.nodeNames,
                proCode: v.nodeCodes,
                sorted: v.sorted,
                content: v.remark,
                qualityParam: "",
                fixture: "",
                measuringTools: "",
                hoursQuota: "",
                salaryQuota: "",
                chengpin: ""
                // skuGetCode: ""
              };
            });
            // processes.forEach((value) => {
            //   let rowInfo = {
            //     name: value.nodeCodes,
            //     proCode: '',
            //     sorted: '',
            //     content: '',
            //     qualityParam: '',
            //     fixture: '',
            //     measuringTools: '',
            //     hoursQuota: '',
            //     salaryQuota: '',
            //     // skuGetCode: ""
            //   };
            //   projectTableData.push(rowInfo);
            // });
            table.reload("productProjectTable", {
              data: projectTableData
            });
          }
        });
      }
    });

    //如果产品属性是加工件 就只有工序
    // if (data.assemblyNatureId === "3") {
    //     bom_tab.style.display = 'none';
    //     bom_content.style.display = 'none';
    //     //加载工序模板

    // } else {
    //     gx_tab.style.display = 'none';
    //     gx_content.style.display = 'none';
    // }

    //移除某一行
    table.on("tool(productProjectTable)", function (obj) {
      if (obj.event === "delProcess") {
        //删除
        obj.del(); // 删除当前行
        // 获取表格数据
        projectTableData = projectTableData.filter((item) => Object.keys(item).length > 0);
        // projectTableData = table.cache['productProjectTable'];
        // console.log('projectTableData=>', projectTableData);

        table.reload("productProjectTable", {
          data: projectTableData
        });
      } else if (obj.event === "checkSku") {
        let skuSelectModel = admin.formOpen({
          type: 1,
          area: ["1480px", "90%"],
          style: "font-size: 20px;",
          title: "客户产品规划管理",
          content: $("#skuSelectDiv").html()
        });
        table.render({
          id: "skuSelectTable",
          elem: "#skuSelectTable",
          url: config.base + "enterprise/product/admin/productDetailWMS/list",
          page: true,

          where: {
            categoryType: "production"
          },
          cols: [
            [
              { type: "radio", title: "选择", width: 60 },
              { type: "numbers", title: "序号", align: "center", width: 60 },
              { field: "skuCode", align: "center", title: "物料编码", width: 120 },
              { field: "skuName", align: "center", title: "物料名称", width: 120 },
              { field: "productTypeName", align: "center", title: "物资类别", width: 120 },
              { field: "productCategoryId", align: "center", title: "自定义分类", width: 120 },
              {
                field: "isDeleted",
                align: "center",
                title: "状态",
                width: 120,
                templet: function (d) {
                  return d.isDeleted ? "停用" : "正常";
                }
              },
              { field: "createTime", align: "center", title: "创建时间", width: 120 },
              { field: "modifierName", align: "center", title: "创建人", width: 120 }
            ]
          ]
        });
        $("#skuSelectConfirm").click(function () {
          //获取选中的skuCode
          let skuObj = table.checkStatus("skuSelectTable").data[0];
          if (skuObj) {
            // 让列表当前这一行的productDetailId等于skuObj.id
            const index = $(obj.tr[0]).data("index");
            table.cache["productProjectTable"][index].productDetailId = skuObj.id;
            table.cache["productProjectTable"][index].skuCode = skuObj.skuCode;
            table.reload("productProjectTable", {
              data: table.cache["productProjectTable"]
            });
            layer.close(skuSelectModel);
          } else {
            layer.msg("请选择物料", { icon: 2, time: 1000 });
          }
        });
      }
    });

    //移除某一行
    table.on("tool(testItemTable)", function (obj) {
      if (obj.event === "delTestItem") {
        //删除
        obj.del(); // 删除当前行
        // 获取表格数据
        testItemData = table.cache["testItemTable"];
        //移除空数据
        testItemData = testItemData.filter((item) => Object.keys(item).length > 0);
        table.reload("testItemTable", {
          data: testItemData
        });
      }
    });

    // 模具图纸
    upload.render({
      elem: "#fileUploadMoldBtn",
      url: config.upload_url + "uploadFile",
      data: {},
      accept: "file",
      exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx",
      headers: { Authorization: "Bearer " + config.getToken() },
      done: function (res) {
        if (res.code == 200) {
          layer.msg(res.msg || "上传成功", { icon: 1 });
          let imagUrl = res.data.url;
          moldName = res.data.fileName;
          fileName = res.data.fileName;
          $("#addMoldHeaderPdf").html(
            '<div class="moldAttachments">' +
              '<div><img name="moldAttachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' +
              moldName +
              "</span></div></div>"
          );
          $("#attachmentMoldPdf").val(imagUrl);

          $(".moldAttachments").click(function () {
            let json = {};
            json.attachment = imagUrl;
            showViewAttachmentModel(json);
          });
        } else if (res.code == 502) {
          return location.replace("admin/login.html");
        } else {
          layer.msg(res.msg || "上传失败", { icon: 2 });
        }
      }
    });

    /**
     * 添加
     */
    $("#addProcessOne").click(function () {
      let newRow = {
        name: "",
        proCode: "",
        sorted: "",
        content: "",
        qualityParam: "",
        fixture: "",
        measuringTools: "",
        hoursQuota: "",
        salaryQuota: ""
        // skuGetCode: ""
      };
      projectTableData.push(newRow);
      table.reload("productProjectTable", {
        data: projectTableData
      });
    });

    //addTestItem
    $("#addTestItem").click(function () {
      let newRow = {
        name: "",
        standard: "",
        utensil: "",
        way: "",
        importance: "",
        detectionRate: ""
      };
      testItemData.push(newRow);
      table.reload("testItemTable", {
        data: testItemData
      });
    });

    //保存检测项点击
    $("#saveTestItem").click(function () {
      let param = {};
      param.skuCode = $("#inSkuCode").val();
      param.productDetailId = $("#productDetailId").val();
      let listInfo = table.cache["testItemTable"];
      let testItemData = [];
      if (Array.isArray(listInfo) && listInfo.length !== 0) {
        listInfo.forEach((item) => {
          let info = {};
          info.name = item.name;
          info.standard = item.standard;
          info.utensil = item.utensil;
          info.way = item.way;
          info.importance = item.importance;
          info.detectionRate = item.detectionRate;
          testItemData.push(info);
        });
      }
      param.testItemData = testItemData;
      admin.req(
        "enterprise/res/admin/planning/proTask/addSkuQuality",
        JSON.stringify(param),
        function (res) {
          layer.closeAll("loading");
          if (res.code == 200) {
            layer.msg(res.msg, { icon: 1 });
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "POST",
        true,
        "application/json; charset=utf-8"
      );
    });
    //保存按钮的点击事件
    $("#saveProjectUp").click(function () {
      let param = {};
      param.templateId = $("#processLineTemplate").val();
      param.skuCode = $("#inSkuCode").val();
      param.productDetailId = $("#productDetailId").val();
      param.tzId = tzId;
      param.tzVersion = tzVersion;
      param.isRelevance = $("#isRelevance").val();
      param.mainSkuId = $("#mainSkuId").val();
      param.fileUrl = $("#attachmentMoldPdf").val();
      param.moldName = moldName;
      param.moldRemark = $("#moldRemark").val();
      let listInfo = table.cache["productProjectTable"];
      let processProjectData = [];
      if (Array.isArray(listInfo) && listInfo.length !== 0) {
        listInfo.forEach((item) => {
          let info = {};
          info.name = item.name;
          info.content = item.content;
          info.proCode = item.proCode;
          info.qualityParam = item.qualityParam;
          info.fixture = item.fixture;
          info.measuringTools = item.measuringTools;
          info.hoursQuota = item.hoursQuota;
          info.salaryQuota = item.salaryQuota;
          info.sorted = item.sorted;
          processProjectData.push(info);
        });
      }
      param.projectTableData = processProjectData;
      console.log("param", param);
      //   echnologyParam/admin/echnologyParam/addSkuProcess
      // enterprise/res/admin/planning/proTask/addSkuProcess
      // admin.req(
      //   'enterprise/echnologyParam/admin/echnologyParam/addSkuProcess',
      //   JSON.stringify(param),
      //   function (res) {
      //     layer.closeAll('loading');
      //     if (res.code == 200) {
      //       layer.msg(res.msg, { icon: 1 });
      //     } else {
      //       layer.msg(res.msg, { icon: 2 });
      //     }
      //   },
      //   'POST',
      //   true,
      //   'application/json; charset=utf-8',
      // );
    });

    //提交校对的点击事件
    $("#submitProofread").click(async function () {
      let formData = {
        drawInfo: {}
      };
      const res = await admin.reqPro("enterprise/res/admin/planning/proTask/detail/" + data.id);
      let resData = res.data;
      console.log("res=>", resData);
      formData.drawInfo.id = resData.drawInfo.id;
      formData.tzId = resData.drawInfo.id;
      formData.templateId = $("#processLineTemplate").val();
      formData.isRelevance = $("#isRelevance").val();
      formData.moldName = fileName;
      formData.fileUrl = $("#attachmentMoldPdf").val();
      formData.moldRemark = $("#moldRemark").val();
      formData.taskId = data.id;
      formData.projectTableData = table.cache["productProjectTable"];
      console.log("formData=>", formData);
      admin.req(
        "enterprise/res/admin/planning/proTask/addSkuProcess",
        JSON.stringify(formData),
        function (res) {
          layer.closeAll("loading");
          if (res.code == 200) {
            layer.msg(res.msg, { icon: 1 });
            table.reload("productTaskTableInfo");
            layer.closeAll("page");
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "POST",
        true,
        "application/json; charset=utf-8"
      );
      // formData.drawInfo.id=
      // let id = data.id;
      // admin.req(
      //   'enterprise/res/admin/planning/proTask/submitProofread/' + id,
      //   {},
      //   function (res) {
      //     layer.closeAll('loading');
      //     if (res.code == 200) {
      //       layer.msg(res.msg, { icon: 1 });
      //       table.reload('productTaskTableInfo', {});
      //       layer.closeAll('page');
      //     } else {
      //       layer.msg(res.msg, { icon: 2 });
      //       layer.closeAll('page');
      //     }
      //   },
      //   'POST',
      // );
    });

    laydate.render({
      elem: "#planGivenTime",
      type: "datetime"
    });

    function showViewAttachmentModel(data) {
      admin.open({
        type: 1,
        area: ["60%", "80%"],
        offset: "65px",
        title: "查看附件",
        content: $("#viewAttachmentHtml").html(),
        success: function () {
          if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
            PDFObject.embed(data.attachment, "#example");
          } else {
            var attachmentHtml = "";
            data.attachment.split(",").forEach((attachmentUrl, index) => {
              attachmentHtml += '<div><div><img src="' + attachmentUrl + '"></div></div>';
            });
            $("#example").html(attachmentHtml);
          }
        }
      });
    }

    //是否关联图纸的点击
    form.on("select(isRelevance)", function (data) {
      if (data.value === "1") {
        $("#moldUploadInfo").show();
      } else {
        $("#moldUploadInfo").hide();
      }
    });

    // 查看附件
    function showViewAttachmentModel2(data) {
      admin.open({
        type: 1,
        area: ["65%", "900px"],
        offset: "35px",
        title: "修改图纸",
        content: $("#viewAttachmentHtml2").html(),
        success: function () {
          console.log("data=>", data);
          if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
            PDFObject.embed(data.attachment, "#example1");
          } else {
            let attachmentHtml = "";
            data.attachment.split(",").forEach((attachmentUrl, index) => {
              attachmentHtml += '<div><img style="max-width: 900px;max-height: 650px" src="' + attachmentUrl + '"></div>';
            });
            $("#example1").html(attachmentHtml);
          }

          //赋值remark
          $("#processDescription").val(data.remark);

          $("#productPicBtn").click(uploadImg("productPicBtn", "example1", "productPic"));

          //保存的点击事件
          form.on("submit(imgModifyFormSubmit)", function (d) {
            layer.load(2);
            let params = {};
            params.type = "1";
            params.orderProNo = data.orderProNo;
            params.remark = $("#processDescription").val();
            params.drawId = data.drawId;
            params.fileUrl = $("#productPic").val();
            params.name = newFileName;
            //新增修改
            console.log("field", d.field);
            admin.req(
              "enterprise/res/admin/proDraw/updateDrawVersion",
              JSON.stringify(params),
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  layer.closeAll("page");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "POST",
              true,
              "application/json; charset=utf-8"
            );
          });
        }
      });
    }

    let newFileName = "";

    // 上传文件
    /*
     * _elem: 上传按钮的名字,
     * pathId: 显示图片容器的名字,
     * inputId: 提交文件路径元素的名字,
     * isMultiple: 是不是多文件(单文件为空或者false,多文件true)
     *
     * */
    function uploadImg(_elem, pathId, inputId, isMultiple) {
      upload.render({
        elem: "#" + _elem,
        url: config.upload_url + "uploadFile",
        data: {},
        accept: "file",
        exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx|apk",
        headers: { Authorization: "Bearer " + config.getToken() },
        done: function (res) {
          if (res.code == 200) {
            layer.msg(res.msg || "上传成功", { icon: 1 });
            let imagUrl = res.data.url;
            newFileName = res.data.fileName;
            if (isMultiple == true) {
              $("#" + pathId).append('<img style="max-width: 900px;max-height: 650px" src="' + imagUrl + '"/>');
              $("#" + inputId).val(imagUrl);
            } else {
              $("#" + pathId).html('<img style="max-width: 900px;max-height: 650px" src="' + imagUrl + '"/>');
              $("#" + inputId).val(imagUrl);
            }
          } else if (res.code == 502) {
            return location.replace("admin/login.html");
          } else {
            layer.msg(res.msg || "上传失败", { icon: 2 });
          }
        }
      });
    }
  });
</script>
